package com.jing.weibo.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.jing.system.comm.BaseController;
import com.jing.weibo.pojo.user.UserInfo;

/**
 * 用户登录的Controller
 * @author jing.yue
 * @version 2012/09/09 1.0.0
 */
public class SecurityInterceptor implements HandlerInterceptor {

	//首页的URL
	private String indexUrl;

	//登录的URL
	private String loginUrl;

	//手机登录
	private String mobileLoginUrl;

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
	}

	/**
	 * 在Controller方法前进行拦截
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute(BaseController.SESSION_USER_KEY);
		String uri = request.getRequestURI();
		if(userInfo == null && !uri.contains(loginUrl) && !uri.contains(mobileLoginUrl)) {
			response.sendRedirect(request.getContextPath() + indexUrl);
			return false;
		}
		return true;
	}

	public String getLoginUrl() {
		return loginUrl;
	}

	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}

	public String getIndexUrl() {
		return indexUrl;
	}

	public void setIndexUrl(String indexUrl) {
		this.indexUrl = indexUrl;
	}

	public String getMobileLoginUrl() {
		return mobileLoginUrl;
	}

	public void setMobileLoginUrl(String mobileLoginUrl) {
		this.mobileLoginUrl = mobileLoginUrl;
	}
}